package com.syh.niceweibo;

import java.util.List;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.syh.niceweibo.common.Constants;
import com.syh.niceweibo.common.Utils;
import com.syh.niceweibo.common.WeiboImpl;
import com.syh.niceweibo.common.infos.HotWeiboInfo;
import com.syh.niceweibo.views.WaterFallLayout;

public class WeiboActivity extends Activity implements OnClickListener {

	private WaterFallLayout mWaterFallLayout;
	private ListView mlvWeiBoContent;

	private ImageView mivTypeImage, mivTypeList;
	private TextView mtvTypeName;
	private NormalAdapter mListAdapter = null;
	private List<HotWeiboInfo> mWeiboList;

	private int type;// default Constants.TYPE_MEINV;
	private boolean mCurrentTypeIsList = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_weibo);

		initView();
		initListener();
		initData();
	}

	private void initView() {
		mWaterFallLayout = (WaterFallLayout) findViewById(R.id.fallsView);
		mivTypeImage = (ImageView) findViewById(R.id.ivTypeImage);
		mivTypeList = (ImageView) findViewById(R.id.ivTypeList);
		mCurrentTypeIsList = Utils.getDisplayTypeIsList(WeiboActivity.this);
		initTypeImage(mCurrentTypeIsList);

		mlvWeiBoContent = (ListView) findViewById(R.id.lvWeiBoContent);
		mtvTypeName = (TextView) findViewById(R.id.tvTypeName);
	}

	private void initListener() {
		mivTypeImage.setOnClickListener(this);
		mivTypeList.setOnClickListener(this);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	private void initData() {
		type = getIntent().getIntExtra(Constants.EXTRA_TYPE_ID,
				Constants.TYPE_MEINV);
		int name_id = getIntent().getIntExtra(Constants.EXTRA_TYPE_NAME,
				R.string.meinv);
		mtvTypeName.setText(name_id);
		new AsyncTask() {

			@Override
			protected Object doInBackground(Object... params) {
				return WeiboImpl.getHotWeiboInfo(WeiboActivity.this, type,
						Constants.TOTAL_CONT, Constants.PAGE_SIZE);
			}

			protected void onPostExecute(Object result) {
				if (result != null) {
					mWeiboList=(List<HotWeiboInfo>) result;
					updateDataList();
				}
			};
		}.execute();
	}

	private void updateDataList() {
		if (mCurrentTypeIsList) {
			mWaterFallLayout.setVisibility(View.GONE);
			mlvWeiBoContent.setVisibility(View.VISIBLE);
			if(mListAdapter==null){
				mListAdapter=new NormalAdapter();
			}
			mListAdapter.updateDataList(mWeiboList);
		} else {
			mWaterFallLayout.setVisibility(View.VISIBLE);
			mlvWeiBoContent.setVisibility(View.GONE);
			mWaterFallLayout.updateDataList(mWeiboList);
		}
	}

	private void initTypeImage(boolean isTypeList) {
		if (isTypeList) {
			mivTypeImage.setImageResource(R.drawable.display_type_image_n);
			mivTypeList.setImageResource(R.drawable.display_type_list_f);
		} else {
			mivTypeImage.setImageResource(R.drawable.display_type_image_f);
			mivTypeList.setImageResource(R.drawable.display_type_list_n);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.ivTypeImage: {
			if (mCurrentTypeIsList) {
				mCurrentTypeIsList = false;
				Utils.setDisplayType(WeiboActivity.this, false);
				initTypeImage(false);
				updateDataList();
			}
			break;
		}
		case R.id.ivTypeList: {
			if (!mCurrentTypeIsList) {
				mCurrentTypeIsList = true;
				Utils.setDisplayType(WeiboActivity.this, true);
				initTypeImage(true);
				updateDataList();
			}

		}
		}

	}

	private class NormalAdapter extends BaseAdapter {

		private List<HotWeiboInfo> dataList;

		@Override
		public int getCount() {
			return dataList!=null?dataList.size():0;
		}

		@Override
		public Object getItem(int arg0) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			return null;
		}

		public void updateDataList(List<HotWeiboInfo> weiboList) {
			dataList = weiboList;
			notifyDataSetChanged();
		}
	}
}
